package Model.Dao;

import java.sql.*;

import Model.Bean.TaskBean;



public class Connecttodatabase {
	public static void main(String[] args){
		String dbUrl="jdbc:mysql://localhost:3306/cs9323";
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection(dbUrl,"root","root");
			String sql="select * from comment;";
			PreparedStatement ps=con.prepareStatement(sql);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				float marks=rs.getFloat(1);
				String content=rs.getString(2);
				String id=rs.getString(3);
				System.out.println("marks "+marks+content+id);
				
			}
			
			rs.close();
			ps.close();
			con.close();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		
	}
	
	public Connection conn(){
		String dbUrl="jdbc:mysql://localhost:3306/cs9323";
		Connection conn=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(dbUrl,"root","root");
			
		}	
		
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}
	
	public void insertintotask(TaskBean td){
		String sql="insert into Task(title,StartDate,ExpireDate,Reward,Content)" +
				" values('"+td.getTitle()+"', '"+td.getStartDate()+"'," +
				"'"+td.getEndDate()+"',"+td.getReward()+", '"+td.getContent()+"');" ;
		String sql2="insert into skill_q values(last_insert_id(),"+td.getCpp()+
				","+td.getJava()+","+td.getCsharp()+","+td.getOracle()+","+td.getPython()+
				","+td.getPhp()+
				","+td.getCommentnum()+
				","+td.getAveragemark()+");";
		Connection con=conn();
		PreparedStatement ps;
		try {
			ps = con.prepareStatement(sql);
			ps.executeUpdate();	
			ps=con.prepareStatement(sql2);
			ps.executeUpdate();
			ps.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	

	

}
